package com.migo.controller;

import com.migo.pojo.EasyUIDataGridResult;
import com.migo.pojo.ItemParam;
import com.migo.service.ItemParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by Administrator on 2017/6/11 0011.
 */
@Controller
@RequestMapping("item/param")
public class ItemParamController {
    private final Logger logger = LoggerFactory.getLogger(ItemParamController.class);
    @Autowired
    private ItemParamService itemParamService;

    @RequestMapping(value = "{itemCatId}",method = RequestMethod.GET)
    public ResponseEntity<ItemParam> getItemParamByCid(@PathVariable("itemCatId") long itemCatId){
        try{
            if(logger.isInfoEnabled()){
                logger.info("查询商品规格参数模板 itemCatId = {}",itemCatId);
            }
            ItemParam example = new ItemParam();
            example.setItemCatId(itemCatId);
            ItemParam itemParam = itemParamService.queryOne(example);
            if(itemParam == null){
                if(logger.isInfoEnabled()){
                    logger.info("所查询商品规格参数模板不存在 itemCatId = {}",itemCatId);
                }
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemParam);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("查询商品规格参数模板失败 itemCatId = {}",itemCatId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @RequestMapping(value = "{itemCatId}",method = RequestMethod.POST)
    public ResponseEntity<Void> addItemParamByItemCatId(@PathVariable("itemCatId") long itemCatId,
                                                        @RequestParam("paramData") String paramData){
        try{
            if(logger.isInfoEnabled()){
                logger.info("新增商品规格参数模板 itemCatId = {},paramData = {}",itemCatId,paramData);
            }
            ItemParam itemParam = new ItemParam();
            itemParam.setId(null);
            itemParam.setItemCatId(itemCatId);
            itemParam.setParamData(paramData);
            itemParamService.save(itemParam);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e){
            logger.error("新增商品规格参数模板失败 itemCatId = {},"+ itemCatId +"paramData = {}",paramData,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResponseEntity<EasyUIDataGridResult> getItemParamList(Integer page,Integer rows){
        try {
            if (logger.isInfoEnabled()) {
                logger.info("查询商品规格模板参数模板 page= {}，rows = {}",page,rows);
            }
            EasyUIDataGridResult result = itemParamService.getItemParamList(page,rows);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            logger.error("查询商品规格参数模板列表出错：page="+page+",rows="+rows,e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

}
